package cn.b.sky.mes.mm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.mm.enums.MoveDirectionECode;
import cn.b.sky.mes.mm.model.InventoryMove;
import cn.b.sky.mes.mm.model.MoveType;
import cn.b.sky.mes.mm.model.StorageLocation;
import cn.b.sky.mes.mm.model.Warehouse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.service.InventoryService;
import cn.b.sky.mes.mm.service.StorageLocationService;
import cn.b.sky.mes.mm.service.WarehouseService;
import cn.b.sky.common.model.DWZResult;

import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

@Controller
@RequestMapping(value = "/inStock/*")
public class InStockController extends SecurityController {
  @Resource
  InventoryService inventoryService;

  @Resource
  StorageLocationService storageLocationService;

  @Resource
  WarehouseService warehouseService;

  @RequestMapping(value = "inStock")
  public String inStock(Model model, String navTabId) {
    List<Warehouse> warehouses = inventoryService.selectUserWarehouseList(getSiteCode(),
        SecurityContextUtil.getCurrentUserName(), MoveDirectionECode.IN.getValue());
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("warehouses", warehouses);
    return "mm/inStock/inStock";
  }

  @RequestMapping(value = "warehouseChange")
  @ResponseBody
  public DWZResult warehouseChange(Model model, String navTabId, String warehousCode) {
    Warehouse warehouse = warehouseService.getWarehouse(warehousCode, getSiteCode());
    List<StorageLocation> locations = null;
    if (warehouse.getLocationManageFlag().intValue() == 1) {
      locations = storageLocationService.selectStorageLocationByWarehouse(getSiteCode(), warehousCode);
    }
    List<MoveType> types = inventoryService.selectMoveTypeByDirection(getSiteCode(),
        SecurityContextUtil.getCurrentUserName(), warehousCode, MoveDirectionECode.IN.getValue());
    Map<String, Object> result = new HashMap<String, Object>();
    result.put("storageLocationCodes", locations);
    result.put("moveTypeCodes", types);
    return ajaxDone(result);
  }

  @RequestMapping(value = "saveInStock")
  @ResponseBody
  public DWZResult saveInStock(Model model, String navTabId, String id, InventoryMove inventoryMove,
      HttpServletRequest request) {
    if (inventoryMove == null) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }

    setCreateInfo(inventoryMove);
    inventoryMove.setSiteCode(getSiteCode());
    inventoryService.doInStock(inventoryMove);
    return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), null, null, null, null);

  }
}
